/*
 * Overview.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.overview;

import java.util.ArrayList;

import android.util.Log;

import de.studiorutton.texttovoice.PlayerWorker;

public class Overview {
	private final static String TAG = "learningwords";
	private static Overview INSTANCE;

	public enum ItemType {
		FIRST_ITEM, LATER_ITEM
	};

	public final static int ITEMS_LEFT = 1;
	public final static int ITEMS_RIGHT = 2;
	public static final String bundle_package = "de.studiorutton.gerengdic.overview";
	public static final String TEXT = "text";

	private OverviewAdapter oa;
	private IOverviewListener ol;
	private PlayerWorker pw;
	private ArrayList<ItemBundle> items = new ArrayList<ItemBundle>();

	public Overview(IOverviewListener ol) {
		this.ol = ol;
		pw = new PlayerWorker(ol.getContext(), ol);
		pw.startThread();
	}
	
	public static Overview get( IOverviewListener ol) {
		if ( INSTANCE == null )
			INSTANCE = new Overview( ol );
		return INSTANCE;
	}
	
	public static void release() {
		INSTANCE.stopThread();
		INSTANCE = null;
	}

	public void setOverviewListener(IOverviewListener ol) {
		this.ol = ol;
		pw.setContextAndListener(ol.getContext(), ol);
	}

	public void finalize() {
		pw.stopThread();
	}

	public OverviewAdapter getOverviewAdapter() {
		if (oa == null) {
			oa = new OverviewAdapter(this);
		}
		oa.setOverviewListener(ol);
		return oa;
	}

	public void parseItem(String item) {
		String langItems[] = item.split("::");

		items.clear();
		parseItemBundle( langItems[0], langItems[1], items );

		oa.notifyDataSetChanged();
	}
	
	private void parseItemBundle( String leftItems, String rightItems, ArrayList<ItemBundle> items ) {
		String leftStrings[] = leftItems.split( "\\|");
		String rightStrings[] = rightItems.split( "\\|");
		
		int stringsIndex = 0;
		int stringsLength;
		if ( leftStrings == null && rightStrings == null )
			stringsLength = 0;
		else if ( leftStrings == null )
			stringsLength = rightStrings.length;
		else if ( rightStrings == null )
			stringsLength = leftStrings.length;
		else if ( leftStrings.length > rightStrings.length )
			stringsLength = leftStrings.length;
		else
			stringsLength = rightStrings.length;
		
		while( stringsIndex < stringsLength ) {
			ItemType type;
			if ( stringsIndex == stringsLength - 1 )
				type = ItemType.FIRST_ITEM;
			else
				type = ItemType.LATER_ITEM;
			
			String leftString;
			if ( stringsIndex >= leftStrings.length )
				leftString = null;
			else
				leftString = leftStrings[stringsIndex];
			
			String rightString;
			if ( stringsIndex >= rightStrings.length )
				rightString = null;
			else
				rightString = rightStrings[stringsIndex];
			
			ArrayList<ItemBundle> subItems = new ArrayList<ItemBundle>();
			parseSubItems( leftString, rightString, type, subItems );
			
			items.addAll( subItems );
			stringsIndex++;
		}
	}
	
	private void parseSubItems( String leftItems, String rightItems, ItemType type, ArrayList<ItemBundle> items ) {
		String leftStrings[] = leftItems.split( ";" );
		String rightStrings[] = rightItems.split( ";" );
		
		int stringsIndex = 0;
		int stringsLength;
		if ( leftStrings == null && rightStrings == null )
			stringsLength = 0;
		else if ( leftStrings == null )
			stringsLength = rightStrings.length;
		else if ( rightStrings == null )
			stringsLength = leftStrings.length;
		else if ( leftStrings.length > rightStrings.length )
			stringsLength = leftStrings.length;
		else
			stringsLength = rightStrings.length;
		
		while( stringsIndex < stringsLength ) {
			ItemType subType;
			if ( stringsIndex == stringsLength - 1 )
				subType = ItemType.FIRST_ITEM;
			else
				subType = ItemType.LATER_ITEM;
			String leftString;
			if ( stringsIndex >= leftStrings.length )
				leftString = null;
			else
				leftString = leftStrings[stringsIndex];
			
			String rightString;
			if ( stringsIndex >= rightStrings.length )
				rightString = null;
			else
				rightString = rightStrings[stringsIndex];
			
			ItemBundle item = new ItemBundle( leftString, rightString, type, subType );
			items.add( item );
			stringsIndex++;
		}
	}

	public ItemBundle getItem( int itemIndex) {
		return items.get(itemIndex);
	}

	public int size() {
		return items.size();
	}

	public void play(int language, String text) {
		pw.playText(language, text);
	}

	public void stopThread() {
		pw.stopThread();
	}

	public class ItemBundle {
		String stringLeft;
		String stringRight;
		ItemType type;
		ItemType subType;

		public ItemBundle(String stringLeft, String stringRight, ItemType type, ItemType subtype) {
			this.stringLeft = stringLeft;
			this.stringRight = stringRight;
			this.type = type;
			this.subType = subtype;
		}
	}

}
